import numpy as np
import matplotlib.pyplot as plt

# x = np.linspace(0, 10, 500)
# dashes = [10, 5, 100, 5]  # 10 points on, 5 off, 100 on, 5 off
#
# fig, ax = plt.subplots()
# line1, = ax.plot(x, np.sin(x), '--', linewidth=2,
#                  label='Dashes set retroactively')
# line1.set_dashes(dashes)
#
# line2, = ax.plot(x, -1 * np.sin(x), dashes=[30, 5, 10, 5],
#                  label='Dashes set proactively')
#
# ax.legend(loc='lower right')

from xlrd import open_workbook

x_data1 = []
y_data1 = []
wb = open_workbook('phase_detector.xlsx')

for s in wb.sheets():
    print('Sheet:', s.name)
    for row in range(s.nrows):
        print('the row is:', row)
        values = []
        for col in range(s.ncols):
            values.append(s.cell(row, col).value)
        print(values)
        x_data1.append(values[0])
        y_data1.append(values[1])


# 绘制图像 V1
def read_xlsx(name):
    wb = open_workbook(name)
    x_data = []
    y_data = []
    for s in wb.sheets():
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                values.append(s.cell(row, col).value)
            x_data.append(values[0])
            y_data.append(values[1])
    return x_data, y_data


x_data, y_data = read_xlsx('my_data.xlsx')
plt.plot(x_data, y_data, 'bo-', label=u"Phase curve", linewidth=1)
plt.title(u"TR14 phase detector")
plt.legend()

plt.xlabel(u"input-deg")
plt.ylabel(u"output-V")

# 绘制图像 V2
from pylab import gca

plt.plot(x_data, y_data, 'bo-', label=u"Phase curve", linewidth=1)

plt.title(u"TR14 phase detector")
plt.legend()

ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

plt.xlabel(u"input-deg")
plt.ylabel(u"output-V")
# x 轴和 y 轴。然后移动这两个轴，使他们的零点对应起来：
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

# 绘制图像 V3
plt.plot(x_data, y_data, 'bo-', label=u"Phase curve", linewidth=1)

plt.annotate('zero point', xy=(180, 0), xytext=(60, 3),
             arrowprops=dict(facecolor='black', shrink=0.05), )

plt.title(u"TR14 phase detector")
plt.legend()

ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

plt.xlabel(u"input-deg")
plt.ylabel(u"output-V")

# 绘制图像 V4
plt.annotate('Close loop point', size=18, xy=(180, 0.1), xycoords='data',
             xytext=(-100, 40), textcoords='offset points',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
             )
plt.annotate(' ', xy=(0, -0.1), xycoords='data',
             xytext=(200, -90), textcoords='offset points',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-.2")
             )
plt.annotate('Zero point in non-monotonic region', size=18, xy=(360, 0), xycoords='data',
             xytext=(-290, -110), textcoords='offset points',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
             )

plt.plot(x_data, y_data, 'b', label=u"Faster D latch and XOR", linewidth=2)

x_data1, y_data1 = read_xlsx('phase_detector.xlsx')
plt.plot(x_data1, y_data1, 'g', label=u"Original", linewidth=2)

x_data2, y_data2 = read_xlsx('phase_detector2.xlsx')
plt.plot(x_data2, y_data2, 'r', label=u"Move the pullup resistor", linewidth=2)

x_data3 = []
y_data3 = []
for i in range(360):
    x_data3.append(i)
    y_data3.append((i - 180) * 0.052 - 0.092)
plt.plot(x_data3, y_data3, 'c', label=u"The Ideal Curve", linewidth=2)

plt.title(u"$2\pi$ phase detector", size=20)
plt.legend(loc=0)  # 显示 label

# 移动坐标轴代码
ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

plt.xlabel(u"$\phi/deg$", size=20)
plt.ylabel(u"$DC/V$", size=20)
# 了解
for i in range(360):
    x_data3.append(i)
    y_data3.append((i - 180) * 0.052 - 0.092)
plt.plot(x_data3, y_data3, 'c', label=u"The Ideal Curve", linewidth=2)

# 绘制图像 V5
plt.annotate('The favorite close loop point', size=16, xy=(1, 0.1), xycoords='data',
             xytext=(-180, 40), textcoords='offset points',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
             )
plt.annotate(' ', xy=(0.02, -0.2), xycoords='data',
             xytext=(200, -90), textcoords='offset points',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-.2")
             )
plt.annotate('Zero point in non-monotonic region', size=16, xy=(1.97, -0.3), xycoords='data',
             xytext=(-290, -110), textcoords='offset points',
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2")
             )

plt.plot(x_data, y_data, 'bo--', label=u"Faster D latch and XOR", linewidth=2)
plt.plot(x_data3, y_data3, 'c', label=u"The Ideal Curve", linewidth=2)

plt.title(u"$2\pi$ phase detector", size=20)
plt.legend(loc=0)  # 显示 label
# 移动坐标轴代码
ax = gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))

plt.xlabel(u"$\phi/rad$", size=20)  # 角度单位为 pi
plt.ylabel(u"$DC/V$", size=20)

plt.xticks([0, 0.5, 1, 1.5, 2], [r'$0$', r'$\pi/2$',
                                 r'$\pi$', r'$1.5\pi$', r'$2\pi$'], size=16)

for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65))

plt.grid(True)
# 对图像与坐标轴相交的部分，做透明化处理
for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65))
# 透明度由其中的参数 alpha=0.65 控制，如果想更透明，就把这个数改到更小，0 代表完全透明，1 代表不透明。而改变横轴坐标显示方式的代码为：
plt.xticks([0, 0.5, 1, 1.5, 2], [r'$0$', r'$\pi/2$',
                                 r'$\pi$', r'$1.5\pi$', r'$2\pi$'], size=16)
